//
//  ViewController.m
//  CollectionViewSlideDelete
//
//  Created by 赵忠鹏 on 2018/5/16.
//  Copyright © 2018年 zhaozhongpeng. All rights reserved.
//

#import "ViewController.h"
#import "SlideCollectionViewCell.h"

@interface ViewController ()<UICollectionViewDataSource,slideDelegate>

@property (nonatomic,strong) UICollectionView *collectionView;


@property (nonatomic,strong) NSIndexPath *indexPath;

@property (nonatomic,strong) NSMutableArray *data;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 50);
    //创建collectionView 通过一个布局策略layout来创建
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    //代理设置
//    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    //注册item类型 这里使用系统的类型
    [self.collectionView registerClass:[SlideCollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];
    [self.view addSubview:self.collectionView];
    
    //添加监听者
    [self addObserver:self forKeyPath: @"indexPath" options: NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context: nil];
   
    
    self.data = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 0; i<10; i++) {
        [self.data addObject:@"1"];
    }
    
}


/**
 *  监听属性值发生改变时回调 保证只有一个 cell 删除按钮显示
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    NSIndexPath *newIndexPath = change[@"new"];
    NSIndexPath *oldIndexPath = change[@"old"];
    if (newIndexPath == oldIndexPath) {
        NSLog(@"相等");
    }else{
        NSLog(@"不相等");
        if (![oldIndexPath isKindOfClass:[NSNull class]]) {
            SlideCollectionViewCell *cell = (SlideCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:oldIndexPath];
            [cell reset];
        }
    }
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.data.count;
    
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SlideCollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];
    cell.mainLabal.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    cell.delegate = self;
    return cell;
}


- (void)deleteButtonClick:(UICollectionViewCell *)currentCell{
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:currentCell];
    if (indexPath.row < self.data.count) {
        
        SlideCollectionViewCell *cell = (SlideCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
        [cell reset];
        
        [self.data removeObjectAtIndex:indexPath.row];
        [self.collectionView reloadData];
    }
}

-(void)swipeGestureLeft:(UICollectionViewCell *)currentCell{
     self.indexPath = [self.collectionView indexPathForCell:currentCell];
}

@end
